package android.pruebas.progressbar1;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SlidingDrawer;
import android.widget.Toast;
import android.widget.ToggleButton;

public class ProgressBarActivity extends Activity {
	
	private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private ProgressBar progressBar3;
    private ProgressBar progressBar4;
    
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final ToggleButton btnInicio = (ToggleButton) findViewById(R.id.btnInicio);
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        progressBar3 = (ProgressBar) findViewById(R.id.progressBar3);
        progressBar4 = (ProgressBar) findViewById(R.id.progressBar4);
        
        mostrarProgreso(false);
        
        
        btnInicio.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	mostrarProgreso(btnInicio.isChecked());
            	
            	
            	
            }
        });  	
    }
    
    
    private void mostrarProgreso(boolean mostrar) {
    	if(mostrar) {
        	
    		progressBar1.setVisibility(ProgressBar.VISIBLE);
    		progressBar2.setVisibility(ProgressBar.VISIBLE);
    		progressBar3.setVisibility(ProgressBar.VISIBLE);
			progressBar4.setProgress(1);
			
			// Start lengthy operation in a background thread
	         new Thread(new Runnable() {
	             public void run() {
	                 while(progressBar4.getProgress()>0 && progressBar4.getProgress() <= progressBar4.getMax()) {
	                	 progressBar4.setProgress(progressBar4.getProgress()+1);
	                	 try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
	                 }
	                 
	                 
	             }
	         }).start();
		   	
    	} else {
    		progressBar1.setVisibility(ProgressBar.GONE);
    		progressBar2.setVisibility(ProgressBar.GONE);
    		progressBar3.setVisibility(ProgressBar.GONE);
    		progressBar4.setProgress(0);
    	}
    }
    
}